package com.fenghongzhang.day015;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Main3Activity extends AppCompatActivity {

    private MyReceiver myReceiver;
    private TextView json1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);


        json1 = (TextView) findViewById(R.id.json);

        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.feng.json");
        registerReceiver(myReceiver,intentFilter);

        Intent intent = new Intent(this, MyIntentService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    //内部的广播接收
    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals("com.feng.json")){
                Bundle extras = intent.getExtras();
                String json = extras.getString("json");
                Toast.makeText(context, json, Toast.LENGTH_SHORT).show();
                json1.setText(json);
            }
        }
    }
}
